package com.yj.ccs.abs.web.management.service;

import com.yj.ccs.abs.boot.pagination.Page;
import com.yj.ccs.abs.common.entity.Area;
import com.yj.ccs.abs.common.entity.Region;
import com.yj.ccs.abs.common.enums.AttachmentType;
import com.yj.ccs.abs.common.enums.CompanyNatureType;
import com.yj.ccs.abs.common.service.RegionService;
import com.yj.ccs.abs.web.common.mapper.AttachmentMapper;
import com.yj.ccs.abs.web.common.service.CommonService;
import com.yj.ccs.abs.web.management.dto.CreateCompanyDto;
import com.yj.ccs.abs.web.management.dto.IdListDto;
import com.yj.ccs.abs.web.management.dto.KeywordDto;
import com.yj.ccs.abs.web.management.dto.UpdateCompanyDto;
import com.yj.ccs.abs.web.management.entity.Company;
import com.yj.ccs.abs.web.management.entity.CompanyDetail;
import com.yj.ccs.abs.web.management.entity.CompanyPageItem;
import com.yj.ccs.abs.web.management.mapper.CompanyMapper;
import com.yj.ccs.abs.web.management.mapper.ManagementPageMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/3/15
 * Author     : Unbelievable9
 **/
@Service
public class CompanyService {

    private final CompanyMapper companyMapper;

    private final ManagementPageMapper pageMapper;

    private final AttachmentMapper attachmentMapper;

    private final RegionService regionService;

    private final CommonService commonService;

    @Autowired
    public CompanyService(CompanyMapper companyMapper,
                          ManagementPageMapper pageMapper,
                          AttachmentMapper attachmentMapper,
                          RegionService regionService,
                          CommonService commonService) {
        this.companyMapper = companyMapper;
        this.pageMapper = pageMapper;
        this.attachmentMapper = attachmentMapper;
        this.regionService = regionService;
        this.commonService = commonService;
    }

    public Company getCompanyById(Long id) {
        return companyMapper.getById(id);
    }

    public Company getCompanyByName(String name) {
        return companyMapper.getByName(name);
    }

    public Page<CompanyPageItem> getCompanyPage(KeywordDto keywordDto) {
        return pageMapper.getCompanyListOnCondition(keywordDto);
    }

    @Transactional
    public void createCompany(CreateCompanyDto createCompanyDto) {
        Company company = new Company();
        BeanUtils.copyProperties(createCompanyDto, company);

        companyMapper.create(company);

        if (createCompanyDto.getLicense() != null) {
            commonService.setupAttachmentList(createCompanyDto.getLicense(), company.getId(), AttachmentType.COMPANY_LICENSE);

            attachmentMapper.batchCreate(createCompanyDto.getLicense());
        }
    }

    public CompanyDetail getCompanyDetail(Company company) {
        CompanyDetail companyDetail = new CompanyDetail();
        BeanUtils.copyProperties(company, companyDetail);
        companyDetail.setNature(CompanyNatureType.fromValue(company.getNature().getValue()));
        companyDetail.setLicense(attachmentMapper.getByIdAndType(company.getId(), AttachmentType.COMPANY_LICENSE.getValue()));

        Area area = regionService.getAreaByCode(companyDetail.getArea_code());
        Region region = regionService.getRegionByAreaCode(area.getCode());

        companyDetail.setArea_name(region.getArea_name());
        companyDetail.setCity_code(area.getCityCode());
        companyDetail.setCity_name(region.getCity_name());
        companyDetail.setProvince_code(area.getProvinceCode());
        companyDetail.setProvince_name(region.getProvince_name());

        return companyDetail;
    }

    @Transactional
    public void updateCompany(UpdateCompanyDto updateCompanyDto) {
        Company company = new Company();
        BeanUtils.copyProperties(updateCompanyDto, company);

        companyMapper.update(company);

        // 营业执照
        attachmentMapper.deleteByIdAndType(company.getId(), AttachmentType.COMPANY_LICENSE.getValue());

        if (updateCompanyDto.getLicense() != null) {
            commonService.setupAttachmentList(updateCompanyDto.getLicense(), company.getId(), AttachmentType.COMPANY_LICENSE);
            attachmentMapper.batchCreate(updateCompanyDto.getLicense());
        }
    }

    @Transactional
    public void deleteCompany(IdListDto idListDto) {
        companyMapper.delete(idListDto);

        attachmentMapper.batchDeleteByIdsAndType(idListDto, AttachmentType.COMPANY_LICENSE.getValue());
    }
}
